package com.xiuxia.bopicturebackend.api.imagesearch.sub;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.xiuxia.bopicturebackend.api.imagesearch.model.ImageSearchResult;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName parseSougouImageSearchResults
 * @Author CHENBO
 * @Date 2025/7/2 15:35
 */
public  class ParseSouGouImageSearchResults {
    public static List<ImageSearchResult> parse(String htmlUrl) {
        List<ImageSearchResult> results = new ArrayList<>();
        Gson gson = new Gson();
        String cookie = "SUV=001FA96227AA3ACE6863954D5A17E324; " +
                "FUV=761577b0da7ee12334c95faa1437c292; " +
                "wuid=1751356800247; " +
                "SNUID=4DB828A58285BB5B046E468B83D68616";
        // 使用 Jsoup 解析 HTML
        Document doc = null;
        try {
            doc = Jsoup.connect(htmlUrl).header("Cookie", cookie).timeout(5000).get();
            // 遍历所有 <script> 标签，找到包含 __INITIAL_STATE__ 的脚本内容
            for (Element script : doc.select("script")) {
                String scriptData = script.data();
                if (scriptData.contains("__INITIAL_STATE__")) {
                    // 提取 JSON 数据
                    Pattern pattern = Pattern.compile("window\\.__INITIAL_STATE__\\s*=\\s*(\\{.*?});", Pattern.DOTALL);
                    Matcher matcher = pattern.matcher(scriptData);

                    if (matcher.find()) {
                        String jsonData = matcher.group(1);

                        // 使用 Gson 解析 JSON
                        JsonObject root = gson.fromJson(jsonData, JsonObject.class);
                        JsonObject risDetail = root.getAsJsonObject("risDetail");
                        JsonObject risResult = risDetail.getAsJsonObject("risResult");
                        JsonObject sim = risResult.getAsJsonObject("sim");

                        JsonArray items = sim.getAsJsonArray("items");
                        for (int i = 0; i < items.size(); i++) {
                            JsonObject item = items.get(i).getAsJsonObject();
                            String picUrl = item.get("pic_url").getAsString();
                            String thumbUrl = item.get("thumbUrl").getAsString();

                            // 创建 ImageSearchResult 并加入列表
                            ImageSearchResult result = new ImageSearchResult();
                            result.setThumbUrl(thumbUrl.replace("\\/", "/")); // 处理转义字符
                            result.setFromUrl(picUrl.replace("\\/", "/"));
                            results.add(result);
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


        return results;
    }
}
